home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elib-006.lha / elib-0.06 / library / stack-f.el < prev    next >
Lisp/Scheme  |  1993-01-24  |  3KB  |  104 lines

  1. ;;;; $Id: stack-f.el,v 0.5 1992/08/19 01:57:58 ceder Exp $
  2. ;;;; This file implements a simple LIFO stack.
  3. ;;;;
  4. ;;;; Copyright (C) 1991, 1992 Free Software Foundation
  5. ;;;;
  6. ;;;; This file is part of the GNU Emacs lisp library, Elib.
  7. ;;;;
  8. ;;;; GNU Elib is free software; you can redistribute it and/or modify
  9. ;;;; it under the terms of the GNU General Public License as published by
  10. ;;;; the Free Software Foundation; either version 1, or (at your option)
  11. ;;;; any later version.
  12. ;;;;
  13. ;;;; GNU Elib is distributed in the hope that it will be useful,
  14. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;;;; GNU General Public License for more details.
  17. ;;;;
  18. ;;;; You should have received a copy of the GNU General Public License
  19. ;;;; along with GNU Emacs; see the file COPYING.  If not, write to
  20. ;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. ;;;; 
  22. ;;;; Author: Inge Wallin
  23. ;;;; 
  24.  
  25. ;;;
  26. ;;; The stack is implemented as a linked list with a tag 'STACK
  27. ;;; as the first element.  All entries and removals are done using
  28. ;;; destructive functions.
  29. ;;;
  30.  
  31.  
  32. ;; Provide the function version and remove the macro version.
  33.  
  34. (provide 'stack-f)
  35. (setq features (delq 'stack-m features))
  36.  
  37.  
  38. ;;; ================================================================
  39.  
  40.  
  41. (defun stack-create ()
  42.   "Create an empty lifo stack."
  43.   (cons 'STACK nil))
  44.  
  45.  
  46. (defun stack-p (stack)
  47.   "Return t if STACK is a stack, otherwise return nil."
  48.   (eq (car-safe stack) 'STACK))
  49.  
  50.  
  51. (defun stack-push (stack element)
  52.   "Push an element onto the stack.
  53. Args: STACK ELEMENT"
  54.   (setcdr stack (cons element (cdr stack))))
  55.  
  56.  
  57. (defun stack-pop (stack)
  58.   "Remove the topmost element from STACK and return it. 
  59. If the stack is empty, return nil."
  60.   (prog1
  61.       (car-safe (cdr stack))
  62.     (setcdr stack (cdr-safe (cdr stack)))))
  63.  
  64.  
  65. (defun stack-empty (stack)
  66.   "Return t if STACK is empty, otherwise return nil."
  67.   (null (cdr stack)))
  68.  
  69.  
  70. (defun stack-top (stack)
  71.   "Return the topmost element of STACK or nil if it is empty."
  72.   (car-safe (cdr stack)))
  73.  
  74.  
  75. (defun stack-nth (stack n)
  76.   "Return nth element of a stack, but don't remove it.
  77. Args: STACK N
  78. If the length of the stack is less than N, return nil.
  79.  
  80. The top stack element has number 0."
  81.   (nth n (cdr stack)))
  82.  
  83.  
  84. (defun stack-all (stack)
  85.   "Return a list of all entries in STACK.
  86. The element last pushed is first in the list."
  87.   (cdr stack))
  88.  
  89.  
  90. (defun stack-copy (stack)
  91.   "Return a copy of STACK.
  92. All entries in STACK are also copied."
  93.   (cons 'STACK (copy-sequence (cdr stack))))
  94.  
  95.  
  96. (defun stack-length (stack)
  97.   "Return the number of elements on STACK."
  98.   (length (cdr stack)))
  99.  
  100.  
  101. (defun stack-clear (stack)
  102.   "Remove all elements from STACK."
  103.   (setcdr stack nil))
  104.